// Copyright (c) 2010 SuccessFactors, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//     * Redistributions of source code must retain the above
//       copyright notice, this list of conditions and the following
//       disclaimer.
//
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials
//       provided with the distribution.
//
//     * Neither the name of the SuccessFactors, Inc. nor the names of
//       its contributors may be used to endorse or promote products
//       derived from this software without specific prior written
//       permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.

package org.owasp.jxt.compiler;

import java.io.File;

/**
 * Compiler -- wrapper class for various Java compilers used to
 * convert Java code to class files.
 *
 * @author Jeffrey Ichnowski
 * @version $Revision: 8 $
 */
public abstract class Compiler {

    protected File _destDir;
    protected File _srcDir;
    protected String _classPath;
    protected ClassLoader _classLoader;
    protected String _source;
    protected String _target;
    protected boolean _debug;

    public final void setDestDir(File dir) {
        _destDir = dir;
    }

    public final void setSourceDirectory(File dir) {
        _srcDir = dir;
    }

    public final void setSource(String source) {
        _source = source;
    }

    public final void setTarget(String target) {
        _target = target;
    }

    public final void setDebug(boolean debug) {
        _debug = debug;
    }

    public final void setClassPath(String classPath) {
        _classPath = classPath;
    }

    public final void setClassLoader(ClassLoader classLoader) {
        _classLoader = classLoader;
    }

    protected final File classToSourceFile(String name) {
        return new File(_srcDir, name.replace('.', '/') + ".java");
    }

    public abstract CompileResults compile(String ... classNames);

    /**
     * Factory method to create the default compiler.
     *
     * @return a new compiler instance
     */
    public static Compiler newInstance() {
        return new EclipseCompiler();
    }

    /**
     * Creates a compiler for the given name.  Names supported:
     * "javac", "ecj", and "jsr199".  The "jsr199" compiler will not
     * be available on VMs prior to JDK1.5.
     *
     * @param name the name of the compiler to instantiate.
     * Specifying <code>null</code> will return the default compiler
     * (same as a call to newInstance()).
     * @return a new compiler for the name.
     * @throw IllegalArgumentException if the compiler name is invalid
     * or unavailable.
     */
    public static Compiler forName(String name) {
        if (null == name) {
            return newInstance();
        } else if ("javac".equals(name)) {
            return new JavacCompiler();
        } else if ("ecj".equals(name)) {
            return new EclipseCompiler();
        } else if ("jsr199".equals(name)) {
            // Use Class.forName since jsr199 isn't available on 1.5.
            try {
                return (Compiler)Class
                    .forName("org.owasp.jxt.compiler.Java6ToolsCompiler")
                    .newInstance();
            } catch (Exception e) {
                throw new IllegalArgumentException(
                    "compiler type '"+name+"' not supported by this VM or library", e);
            }
        } else {
            throw new IllegalArgumentException(
                "Unknown or unsupported compiler type: "+name);
        }
    }

} // Compiler
